﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using EvaluationsTableAdapters;

/// <summary>
/// Summary description for EvaluationsBLL
/// </summary>
[System.ComponentModel.DataObject]
public class EvaluationsBLL
{
    ILE_Global ileGlobal = new ILE_Global();

    private EvaluationsTableAdapter _evaluationsAdapter = null;
    protected EvaluationsTableAdapter EvaluationsAdapter
    {
        get
        {
            if (_evaluationsAdapter == null)
                _evaluationsAdapter = new EvaluationsTableAdapter();

            return _evaluationsAdapter;
        }
    }

    private Evaluations_SubmissionsTableAdapter _submissionsAdapter = null;
    protected Evaluations_SubmissionsTableAdapter SubmissionsAdapter
    {
        get
        {
            if (_submissionsAdapter == null)
                _submissionsAdapter = new Evaluations_SubmissionsTableAdapter();

            return _submissionsAdapter;
        }
    }

    private Relations_Evaluations_Courses_SectionTableAdapter _relationsAdapter = null;
    protected Relations_Evaluations_Courses_SectionTableAdapter RelationsAdapter
    {
        get
        {
            if (_relationsAdapter == null)
                _relationsAdapter = new Relations_Evaluations_Courses_SectionTableAdapter();

            return _relationsAdapter;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, true)]
    public Evaluations.EvaluationsDataTable GetEvaluations()
    {
        return EvaluationsAdapter.GetEvaluations();
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Evaluations.EvaluationsRow GetEvaluation(int EvaluationID)
    {
        try
        {
            return EvaluationsAdapter.GetEvaluation(EvaluationID)[0];
        }
        catch
        {
            ileGlobal.RecordError("EvaluationsBLL.cs:GetEvaluation:000", "Could not load Evaluation", 3);
            return null;
        }
    }

    public int InsertEvaluation(string Introduction, string Questions)
    {
        try
        {
            return Convert.ToInt32(EvaluationsAdapter.InsertEvaluation(Introduction, Questions));
        }
        catch
        {
            ileGlobal.RecordError("EvaluationsBLL.cs:InsertEvaluation:000", "Failed to insert Evaluation", 3);
            return 0;
        }
    }

    public bool UpdateEvaluation(string Introduction, string Questions, int EvaluationID)
    {
        try
        {
            return (EvaluationsAdapter.Update(Introduction, Questions, EvaluationID) == 1);
        }
        catch
        {
            ileGlobal.RecordError("EvaluationsBLL.cs:UpdateEvaluation:000", "Failed to update Evaluation", 3);
            return false;
        }
    }

    public Evaluations.Evaluations_SubmissionsDataTable GetEvaluationSubmissionsByCourseIdAndSectionNumber(int CourseID, int SectionNumber)
    {
        return SubmissionsAdapter.GetEvaluationSubmissionsByCourseIdAndSectionNumber(CourseID, SectionNumber);
    }

    public Evaluations.Evaluations_SubmissionsRow GetEvaluationSubmissionsByCourseIdAndSectionNumberAndUserId(int CourseID, int SectionNumber, int UserID)
    {
        try
        {
            return SubmissionsAdapter.GetEvaluationSubmission(CourseID, SectionNumber, UserID)[0];
        }
        catch
        {
            return null;
        }
    }

    public bool InsertEvaluationSubmission(int EvaluationID, int UserID, int CourseID, int SectionNumber, string Answers)
    {
        try
        {
            return SubmissionsAdapter.Insert(EvaluationID, UserID, CourseID, SectionNumber, Answers) == 1;
        }
        catch
        {
            return false;
        }
    }
    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Evaluations._Relations_Evaluations_Courses_SectionDataTable GetRelations()
    {
        Evaluations._Relations_Evaluations_Courses_SectionDataTable relations = RelationsAdapter.GetRelations();
        return relations;
    }

    public Evaluations._Relations_Evaluations_Courses_SectionDataTable GetRelationsByCourseIdAndSectionNumber(int CourseID, int SectionNumber)
    {
        Evaluations._Relations_Evaluations_Courses_SectionDataTable relations = RelationsAdapter.GetRelationsByCourseIdAndSectionNumber(CourseID, SectionNumber);
        return relations;
    }

    public bool InsertRelation(int EvaluationID, int CourseID, int SectionNumber)
    {
        try
        {
            return RelationsAdapter.InsertEvalRelation(EvaluationID, CourseID, SectionNumber) == 1;
        }
        catch
        {
            return false;
        }
    }
    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Delete, false)]
    public bool DeleteEvalRelation(int CourseID, int SectionNumber, int original_CourseID, int original_SectionNumber)
    {
        try
        {
            //Evaluations._Relations_Evaluations_Courses_SectionRow relation =
                //GetRelationsByCourseIdAndSectionNumber(original_CourseID, original_SectionNumber)[0];
            //return EvaluationsAdapter.Delete(relation.EvaluationID) == 1;
            return RelationsAdapter.DeleteEvalRelationByCourseIdAndSectionNumber(original_CourseID, original_SectionNumber) == 1;
        }
        catch
        {
            return false;
        }
    }

    public bool RelationsIsDuplicated(int CourseID, int SectionNumber)
    {
        return (RelationsAdapter.IsDuplicated(CourseID, SectionNumber).Value >= 1);
    }
}
